package com.example.its2020008;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;


import javax.sql.CommonDataSource;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity  {

    private TextView textView;
    private Button btnSetting;
    private EditText editUser;
    private EditText editPassword;
    private CheckBox chkMainPassword;
    private CheckBox chkMainName;
    private Button btnLogin;
    private Button btnZhuce;
    private String url = "http://121.9.253.237:8888/traffic/api/v2/";
    OkHttpClient okHttpClient = new OkHttpClient();
    private SharedPreferences sp;
    private ImageView imageView;
    private RelativeLayout relativeLayout2;
    private ImageView imageView2;
    private CheckBox chkMainpassword;

    @Override
    protected void onResume() {
        super.onResume();
        boolean isSavePwd = sp.getBoolean("isSavsPwd", false);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        btnZhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(LoginActivity.this, register.class);
                startActivity(i);
            }
        });
        btnSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                netsettingDialog();
            }
        });
        sp = getSharedPreferences("login", MODE_PRIVATE);
        chkMainName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                SharedPreferences.Editor editor = sp.edit();
                editor.putBoolean("chkName", b);
                editor.commit();
                if (b) {
                    chkMainName.setChecked(true);
                    chkMainName.setEnabled(false);
                } else {
                    chkMainName.setEnabled(true);
                }
            }
        });
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (editUser.getText().toString().equals("admin")&&editPassword.getText().toString().equals("123456")){
                    // 用户名、密码正确，成功登陆
                    Toast.makeText(LoginActivity.this,"登陆成功!",Toast.LENGTH_SHORT).show();
                    // 实现页面跳转
                    Intent intent = new Intent();
                    intent.setClass(LoginActivity.this,mLogin.class);
                    // 存储当前用户名，用于工具界面显示信息
                    intent.putExtra("username",editUser.getText().toString());
                    startActivity(intent);
                } else if (editUser.getText().toString().equals("")||editPassword.getText().toString().equals("")){
                    // 用户名、密码不能为空
                    Toast.makeText(LoginActivity.this,"用户名/密码不能为空!",Toast.LENGTH_SHORT).show();
                } else {
                    // 用户名、密码不正确
                    Toast.makeText(LoginActivity.this,"登录失败，密码或用户名错误!",Toast.LENGTH_SHORT).show();
                }
            }
        });


//                String user = editUser.getText().toString().trim();
//                String password = editPassword.getText().toString().trim();
//                String md5Psw = MD5Utils.md5(password);
//                if (TextUtils.isEmpty(user)) {
//                    Toast.makeText(LoginActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
//                    return;
//                } else if (TextUtils.isEmpty(password)) {
//                    Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
//                    return;
//                } else if (md5Psw.equals(sp)) {
//                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
//                    Intent data = new Intent();
//                    data.putExtra("isLogin", true);
//                    setResult(RESULT_OK, data);
//                    LoginActivity.this.finish();
//                    return;
//                }
//

//
//        btnLogin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                String userName = editUser.getText().toString().trim();
//                String password = editPassword.getText().toString().trim();
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        JsonObject jsonObject = new JsonObject();
//                        jsonObject.addProperty("UserName", userName);
//                        jsonObject.addProperty("UserPassword", password);
//                        String json = jsonObject.toString();
//                        MediaType mediaType = MediaType.Companion.parse("application/json;charset=utf-8");
//                        RequestBody requestBody = RequestBody.create(json, mediaType);
//                        Request request = new Request.Builder()
//                                .url(url)
//                                .post(requestBody)
//                                .build();
//
//                        Call call = okHttpClient.newCall(request);
//                        call.enqueue(new Callback() {
//                            @Override
//                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
//                            }
//
//                            @Override
//                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
//                                String result = response.body().string();
//                                Log.i("TAG", "onResponse:" + result);
//
//                            }
//                        });
//                    }
//                }).start();
//            }
//        });
    }
    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        btnSetting = (Button) findViewById(R.id.btn_Setting);
        editUser = (EditText) findViewById(R.id.edit_user);
        editPassword = (EditText) findViewById(R.id.edit_password);
        chkMainPassword = (CheckBox) findViewById(R.id.chk_Mainpassword);
        chkMainName = (CheckBox) findViewById(R.id.chk_MainName);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnZhuce = (Button) findViewById(R.id.btn_zhuce);
        imageView = (ImageView) findViewById(R.id.imageView);
        relativeLayout2 = (RelativeLayout) findViewById(R.id.relativeLayout2);
        chkMainpassword = (CheckBox) findViewById(R.id.chk_Mainpassword);
    }
    private  void  netsettingDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
        View view = LayoutInflater.from(LoginActivity.this).inflate(
                R.layout.netsetting_dialog,null,false );
        builder.setView(view);
        final Dialog dialog = builder.create();
        EditText edtIP=view.findViewById(R.id.edt_ip);
        EditText edtPort=view.findViewById(R.id.edt_port);
        Button btnSave = view.findViewById(R.id.btn_save);
        Button btnCancel = view.findViewById(R.id.btn_cancle);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }


}